package handlers

import (
	"net/http"

	"github.com/devops-smartbot/devops-smartbot/services/api-gateway/internal/models"
	"github.com/devops-smartbot/devops-smartbot/services/api-gateway/internal/repository"

	"github.com/gin-gonic/gin"
)

type SettingsHandler struct {
	repo *repository.SettingsRepository
}

func NewSettingsHandler(repo *repository.SettingsRepository) *SettingsHandler {
	return &SettingsHandler{repo: repo}
}

// System Settings
func (h *SettingsHandler) GetSystemSettings(c *gin.Context) {
	settings, err := h.repo.GetSystemSettings(c.Request.Context())
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, settings)
}

func (h *SettingsHandler) UpdateSystemSettings(c *gin.Context) {
	var settings models.SystemSettings
	if err := c.ShouldBindJSON(&settings); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// TODO: Get user from JWT token
	settings.UpdatedBy = "admin"

	if err := h.repo.UpdateSystemSettings(c.Request.Context(), &settings); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "System settings updated successfully",
		"data":    settings,
	})
}

// Notification Settings
func (h *SettingsHandler) GetNotificationSettings(c *gin.Context) {
	// TODO: Get userID from JWT token
	userID := c.DefaultQuery("userId", "admin")

	settings, err := h.repo.GetNotificationSettings(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, settings)
}

func (h *SettingsHandler) UpdateNotificationSettings(c *gin.Context) {
	var settings models.NotificationSettings
	if err := c.ShouldBindJSON(&settings); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// TODO: Get userID from JWT token
	if settings.UserID == "" {
		settings.UserID = "admin"
	}

	if err := h.repo.UpdateNotificationSettings(c.Request.Context(), &settings); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "Notification settings updated successfully",
		"data":    settings,
	})
}

// Integrations
func (h *SettingsHandler) ListIntegrations(c *gin.Context) {
	integrations, err := h.repo.ListIntegrations(c.Request.Context())
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	if integrations == nil {
		integrations = []models.Integration{}
	}

	c.JSON(http.StatusOK, gin.H{
		"data": integrations,
	})
}

func (h *SettingsHandler) GetIntegration(c *gin.Context) {
	id := c.Param("id")
	integration, err := h.repo.GetIntegration(c.Request.Context(), id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Integration not found"})
		return
	}
	c.JSON(http.StatusOK, integration)
}

func (h *SettingsHandler) CreateIntegration(c *gin.Context) {
	var integration models.Integration
	if err := c.ShouldBindJSON(&integration); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err := h.repo.CreateIntegration(c.Request.Context(), &integration); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{
		"success": true,
		"message": "Integration created successfully",
		"data":    integration,
	})
}

func (h *SettingsHandler) UpdateIntegration(c *gin.Context) {
	id := c.Param("id")
	var integration models.Integration
	if err := c.ShouldBindJSON(&integration); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err := h.repo.UpdateIntegration(c.Request.Context(), id, &integration); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "Integration updated successfully",
		"data":    integration,
	})
}

func (h *SettingsHandler) DeleteIntegration(c *gin.Context) {
	id := c.Param("id")
	if err := h.repo.DeleteIntegration(c.Request.Context(), id); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "Integration deleted successfully",
	})
}

// User Preferences
func (h *SettingsHandler) GetUserPreference(c *gin.Context) {
	// TODO: Get userID from JWT token
	userID := c.DefaultQuery("userId", "admin")

	pref, err := h.repo.GetUserPreference(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, pref)
}

func (h *SettingsHandler) UpdateUserPreference(c *gin.Context) {
	var pref models.UserPreference
	if err := c.ShouldBindJSON(&pref); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// TODO: Get userID from JWT token
	if pref.UserID == "" {
		pref.UserID = "admin"
	}

	if err := h.repo.UpdateUserPreference(c.Request.Context(), &pref); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"message": "User preferences updated successfully",
		"data":    pref,
	})
}
